#!/bin/bash

echo "🔧 修复uni-app构建问题..."

# 创建修复版本的测试项目
mkdir -p test-uniapp-fix
cd test-uniapp-fix

# 创建正确的uni-app项目package.json
cat > package.json << 'EOF'
{
  "name": "test-uniapp-project",
  "version": "1.0.0",
  "scripts": {
    "serve": "npm run dev:h5",
    "build": "npm run build:h5",
    "build:h5": "cross-env NODE_ENV=production UNI_PLATFORM=h5 vue-cli-service uni-build",
    "build:mp-weixin": "cross-env NODE_ENV=production UNI_PLATFORM=mp-weixin vue-cli-service uni-build",
    "dev:h5": "cross-env NODE_ENV=development UNI_PLATFORM=h5 vue-cli-service uni-serve",
    "dev:mp-weixin": "cross-env NODE_ENV=development UNI_PLATFORM=mp-weixin vue-cli-service uni-build --watch"
  },
  "dependencies": {
    "@dcloudio/uni-app": "3.0.0-alpha-4010020231201001",
    "@dcloudio/uni-h5": "3.0.0-alpha-4010020231201001",
    "@dcloudio/uni-mp-weixin": "3.0.0-alpha-4010020231201001",
    "vue": "^3.2.47"
  },
  "devDependencies": {
    "@dcloudio/cli-shared": "3.0.0-alpha-4010020231201001",
    "@dcloudio/vite-plugin-uni": "3.0.0-alpha-4010020231201001",
    "@dcloudio/vue-cli-plugin-uni": "5.0.0-alpha-4010020231201001",
    "@vue/cli-service": "~5.0.8",
    "sass": "^1.32.8",
    "sass-loader": "^12.0.0",
    "typescript": "^4.5.4",
    "vite": "^4.0.0",
    "cross-env": "^7.0.3"
  },
  "browserslist": [
    "Android >= 4.4",
    "ios >= 9"
  ]
}
EOF

# 创建pages.json
cat > src/pages.json << 'EOF'
{
  "pages": [
    {
      "path": "pages/index/index",
      "style": {
        "navigationBarTitleText": "首页"
      }
    }
  ],
  "globalStyle": {
    "navigationBarTextStyle": "black",
    "navigationBarTitleText": "uni-app",
    "navigationBarBackgroundColor": "#F8F8F8",
    "backgroundColor": "#F8F8F8"
  }
}
EOF

# 创建manifest.json
cat > src/manifest.json << 'EOF'
{
  "name": "test-uniapp",
  "appid": "__UNI__test123",
  "description": "",
  "versionName": "1.0.0",
  "versionCode": "100",
  "transformPx": false,
  "mp-weixin": {
    "appid": "",
    "setting": {
      "urlCheck": false,
      "es6": true,
      "enhance": true,
      "postcss": true,
      "preloadBackgroundData": false,
      "minified": true,
      "newFeature": false,
      "coverView": true,
      "nodeModules": false,
      "autoAudits": false,
      "showShadowRootInWxmlPanel": true,
      "scopeDataCheck": false,
      "uglifyFileName": false,
      "checkInvalidKey": true,
      "checkSiteMap": true,
      "uploadWithSourceMap": true,
      "compileHotReWrite": false,
      "lazyloadPlaceholderEnable": false,
      "useMultiFrameRuntime": true,
      "useApiHook": true,
      "useApiHostProcess": true,
      "babelSetting": {
        "ignore": [],
        "disablePlugins": [],
        "outputPath": ""
      },
      "enableEngineNative": false,
      "useIsolateContext": true,
      "userConfirmedBundleSwitch": false,
      "packNpmManually": false,
      "packNpmRelationList": [],
      "minifyWXSS": true,
      "disableUseStrict": false,
      "minifyWXML": true,
      "showES6CompileOption": false,
      "useCompilerPlugins": false
    },
    "usingComponents": true,
    "nvueStyleCompiler": "uni-app",
    "compilerVersion": 3,
    "splashscreen": {
      "alwaysShowBeforeRender": true,
      "waiting": true,
      "autoclose": true,
      "delay": 0
    },
    "optimize": {
      "subPackages": true
    },
    "uniStatistics": {
      "enable": false
    }
  }
}
EOF

# 创建简化版Dockerfile，专注解决构建问题
cat > Dockerfile.fix << 'EOF'
FROM --platform=linux/amd64 node:18.18.2-alpine

WORKDIR /app

# 安装系统依赖
RUN apk add --no-cache \
    python3 \
    make \
    g++ \
    git

# 设置环境变量
ENV PYTHON=/usr/bin/python3

# 配置npm
RUN npm config set registry https://registry.npmmirror.com/

# 复制项目文件
COPY package.json ./
COPY src ./src

# 安装依赖
RUN npm install

# 构建项目
RUN npm run build:mp-weixin

# 检查构建结果
RUN echo "检查构建后的目录结构:"
RUN ls -la
RUN ls -la dist/ 2>/dev/null || echo "dist目录不存在"
RUN ls -la unpackage/ 2>/dev/null || echo "unpackage目录不存在"

# 搜索构建产物
RUN find . -name "*.wxml" -o -name "*.wxss" -o -name "*.js" | head -10

CMD ["tail", "-f", "/dev/null"]
EOF

echo "✅ 创建了修复版测试项目"
echo "🔧 使用新版本构建..."
docker build -f Dockerfile.fix -t test-uniapp-fix .

if [ $? -eq 0 ]; then
    echo "✅ Docker构建成功！"
    echo "🔍 进入容器检查..."
    docker run --rm test-uniapp-fix ls -la /app/
else
    echo "❌ Docker构建失败，尝试调试..."
fi

# 清理
cd ..
rm -rf test-uniapp-fix
